var Server = pi.base.extend({
  "$Init":function(_id,_url){
    this.environment.setId(_id).environment.setName(_url).environment.setUrl(_url);
    return this;
  },
  "create":function(){
    this.disconnect();
    new Request(this.environment.getUrl()+"/table/create/callback=main.engine.environment.getTable().setup&name={0}&location={1}?cacheForce={2}".format(
        Request.filter(main.engine.element.getNicknameInput().element.value),
        Request.filter(main.engine.element.getLocationInput().element.value),
        Number(new Date)
      )
    ).send();
  },
  "disconnect":function(){
    console.log(".. disconnecting..");
    if(this.environment.getRequest()){
      this.environment.getRequest().abort();
      this.environment.setRequest(null);
    }
    this.environment.setIsLocked(false);
  },
  "join":function(_id){
    this.disconnect();
    new Request(this.environment.getUrl()+"/table/join/callback=main.engine.environment.getTable().setup&name={0}&location={1}&tableId={2}?cacheForce={3}".format(
        Request.filter(main.engine.element.getNicknameInput().element.value),
        Request.filter(main.engine.element.getLocationInput().element.value),
        _id,
        Number(new Date)
      )
    ).send();
  },
  "listen":{
    "table":function(){
      console.log(",, request for getting available tables from ",this._parent_.environment.getUrl()," lock?",this._parent_.environment.getIsLocked());
      if(!this._parent_.environment.getIsLocked()&&main.engine.environment.getTable()==null){
        if(this._parent_.environment.getPlayerLimit()==-1){
          console.log(",, first connection.updating server info box.");
          main.engine.element.getServerInfoBox().update(main.text.connecting+this._parent_.environment.getUrl()+"...");
        }
        
        this._parent_.environment.setRequest(
          new Request(
            this._parent_.environment.getUrl()+"/table/list/callback=main.engine.event.readTableList&serverIndex="+this._parent_.environment.getId()+"?cacheForce="+Number(new Date)
          ).send()
        );
        this._parent_.environment.setIsLocked(true);
      }
    },
    "move":function(){ 
      console.log(".. creating new move listener..");
      if(!this._parent_.environment.getIsLocked()&&main.engine.environment.getTable().environment.getOpponent()!=null){
        main.engine.environment.getTable().environment.setStatusText(main.text.waitingOpponentsMove,'Blue');
        this._parent_.environment.setRequest(
          new Request(
            this._parent_.environment.getUrl()+"/table/listenMove/callback=main.engine.environment.getTable().listenMove&tableId="+main.engine.environment.getTable().environment.getId()+"&mintime="+main.engine.environment.getTable().environment.getServerTime()+"?cacheForce="+Number(new Date)
          ).send()
        );
        console.log(".. sent.");
        this._parent_.environment.setIsLocked(true);
      }
    },
    "opponent":function(){
      console.log(",,,, request for listening opponent",this._parent_.environment.getIsLocked());
      if(!this._parent_.environment.getIsLocked()&&main.engine.environment.getTable().environment.getOpponent()==null){
        main.engine.environment.getTable().environment.setStatusText(main.text.waitingForOpponent,'Blue');
        this._parent_.environment.setRequest(
          new Request(
            this._parent_.environment.getUrl()+"/table/listenOpponent/callback=main.engine.environment.getTable().listenOpponent&tableId="+main.engine.environment.getTable().environment.getId()+"?cacheForce="+Number(new Date)
          ).
          send()
        );
        this._parent_.environment.setIsLocked(true);
      }
    }
  },
  "move":function(_id,_x,_y){
    new Request(
      this.environment.getUrl()+"/table/move/tableId="+main.engine.environment.getTable().environment.getId()+"&pieceId="+_id+"&x="+_x+"&y="+_y
    ).send();
  },
  "read":function(){
    this.environment.setTable(eval( this.environment.getRequest().api.responseText ));
    this.update();
  },
  "update":function(){
    this.environment.getElement().update(
      ((this.environment.getPlayerLimit()>-1&&"<span class='PlStat'>({1}/{2})</span>"||"")+"{0}").format( this.environment.getName(), this.environment.getPlayerCount(), this.environment.getPlayerLimit() )
    );
    if(main.engine.element.getServerInfoBox()&&main.engine.environment.getActiveServer()==this){
      var info = "\
<span class='Blue'><strong>%(lang_server):</strong>%(name)</span>\
<span class='Green'><strong>%(lang_location):</strong>%(location)</span>\
<span class='Red'><strong>%(lang_owner):</strong>%(ownerText)</span>\
<br /><span class='Grey'><strong>%(lang_url):</strong>%(url)</span>\
        ".format({
          "name":this.environment.getName(),
          "location":this.environment.getLocation(),
          "ownerText":this.environment.getOwnerText(),
          "url":this.environment.getUrl(),
          "lang_server":main.text.server,
          "lang_location":main.text.location,
          "lang_owner":main.text.owner,
          "lang_url":main.text.url
        });
        
      if(main.engine.element.getServerInfoBox().element.innerHTML!=info)
        main.engine.element.getServerInfoBox().update(info);
    }
  },
  "updateProfile":function(){
    new Request(
      main.engine.environment.getMasterServer().environment.getUrl()+"/auth/update/name="+Request.filter(main.engine.element.getNicknameInput().element.value)+"&location="+Request.filter(main.engine.element.getLocationInput().element.value)
    ).send();
  },
  "environment":{
    "_element":null, "_id":-1, "_location":"", "_isLocked":false, "_playerLimit":-1, "_playerCount":-1, "_name":"", "_ownerText":"", "_table":[], "_url":"", "_request":null, "_time":0,
    "setName":function(_value){
      this._setName(_value.substring(0,18));
      return this._parent_;
    }
  },
  "event":{
    "table_load":function(){},
    "table_read_error":function(){
      main.engine.element.getServerInfoBox("Could not connect to the picked server.")
    }
  }
});
